package cachex

import (
	"errors"
	"sync"

	"github.com/tal-tech/go-zero/core/stores/cache"
	"github.com/tal-tech/go-zero/core/syncx"
)

var (

	// can't use one SharedCalls per conn, because multiple conns may share the same cache key.
	exclusiveCalls = syncx.NewSharedCalls()
	stats          = cache.NewStat("normal")
	ErrorNotFound  = errors.New("cache not found")
	once           sync.Once
	commonCache    cache.Cache
)

func GetCache(c cache.CacheConf) cache.Cache {
	once.Do(func() {
		commonCache = newZeroCache(c)
	})
	return commonCache
}

func newZeroCache(c cache.CacheConf) cache.Cache {
	return cache.New(c, exclusiveCalls, stats, ErrorNotFound)
}
